如何检测两个控件是否"接触"?

来源:百度知道 编辑:UC知道 时间:2024/06/11 10:41:36
RT
说得简单些

假定有两个命令按钮

Private Sub Form_Load()
If IsTangency(Command1, Command2) Then MsgBox "接触"
End Sub

Public Function IsTangency(ctr1 As Control, ctr2 As Control)
Dim b As Boolean
b = PntInRec(ctr1.Left, ctr1.Top, ctr2.Left, ctr2.Height, ctr2.Width, ctr2.Height)
b = b Or PntInRec(ctr1.Left, ctr1.Top + ctr1.Height, ctr2.Left, ctr2.Height, ctr2.Width, ctr2.Height)
b = b Or PntInRec(ctr1.Left + ctr1.Width, ctr1.Top, ctr2.Left, ctr2.Height, ctr2.Width, ctr2.Height)
b = b Or PntInRec(ctr1.Left + ctr1.Width, ctr1.Top + ctr1.Height, ctr2.Left, ctr2.Height, ctr2.Width, ctr2.Height)
IsTangency = b
End Function

Public Function PntInRec(x As Integer, y As Integer, x1 As Integer, y1 As Integer, Width As Integer, Height As Integer)
PntInRec = x >= x1 And y >= y1 And x <= x1 + Width And y <= y1 + Height
End Function

不知道你说什么意思